package com.voxel.sense.infomsg.module.task.execution;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageButton;

import com.example.com.common.app.BaseActivity;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.SnackbarUtils;
import com.example.com.common.utils.ToastUtil;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.task.execution.file.TaskFileOperationActivity;
import com.voxel.sense.infomsg.module.task.execution.report.list.ReportListActivity;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.net.helper.TaskNetHelper;

/**
 * 执行任务主界面
 */
public class TaskResultActivity extends BaseActivity implements DataSource.Callback<BaseEntity> {
    @BindView(R.id.publish_report)
    ImageButton publish_report;
    @BindView(R.id.publish_file)
    ImageButton publish_file;
    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    private String mNumb;

    public static final String CAN_SUBMIT_TASK = "1";
    private String mStaues;
    private int operaType;

    public static void show(Context context, String taskActioStatu, String taskNumb, String TaskId, int operType) {
        Intent intent = new Intent(context, TaskResultActivity.class);
        intent.putExtra(IntentConstants.TASK_REPORT_STATUS, taskActioStatu);
        intent.putExtra(IntentConstants.TASK_NUMB, taskNumb);
        intent.putExtra(IntentConstants.STRING_ID, TaskId);
        intent.putExtra(IntentConstants.OPEAR_TYPE, operType);
        context.startActivity(intent);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_task_result;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText("执行反馈");
        mNumb = getIntent().getStringExtra(IntentConstants.TASK_NUMB);

        operaType = getIntent().getIntExtra(IntentConstants.OPEAR_TYPE, 0);

    }

    private void showPassBtn() {
        switch (operaType){
            case 0:
                titlebar.setRightVisible(false);
                break;
            case 1:
                titlebar.setRightVisible(true);
                break;
            default:
                titlebar.setRightVisible(true);
                break;
        }
    }

    @Override
    protected void initEven() {
        mStaues = getIntent().getStringExtra(IntentConstants.TASK_REPORT_STATUS);
        String id = getIntent().getStringExtra(IntentConstants.STRING_ID);


        titlebar.setRightTextAndLis("提交审核", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (CAN_SUBMIT_TASK.equals(mStaues)) {
                    TaskNetHelper.doSubmitAudit(mNumb, id, TaskResultActivity.this);
//                } else {
//                    showSnackBar("请先上传报表");
//                }
            }
        });
        showPassBtn();
    }

    @OnClick({R.id.publish_report, R.id.publish_file})
    void OnClickEvent(View v) {
        if (v.getId() == R.id.publish_report) {
            ReportListActivity.show(this, mNumb);
        } else if (v.getId() == R.id.publish_file) {
            TaskFileOperationActivity.show(this, mNumb, operaType);
        }
    }

    @Override
    public void onDataLoaded(BaseEntity data, int reqId) {
        if (data.isSuccess()) {
            titlebar.setRightVisible(false);
            ToastUtil.showToastLong(Factory.app(), "任务提交成功");
            EventBus.getDefault().post(new EventEntity(EventBusConstants.TASK_ACTION_FINISH, mNumb));
            finish();
        } else {
            showSnackBar("提交失败，请稍后再试");
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        showSnackBar("提交失败，请稍后再试");
    }

    private void showSnackBar(String str) {
        SnackbarUtils.with(titlebar).setMessage(str).showWarning();
    }

    @Subscribe
    public void EventThread(EventEntity entity) {
        if (EventBusConstants.UP_REPORT_SUCCESS.equals(entity.key)) {
            //表单上传成功通知
            mStaues = CAN_SUBMIT_TASK;
        }
        if (EventBusConstants.TASK_RESULT_REPORT_DELLETE.equals(entity.key)) {
            //清除所有表单通知，清除后不能提交任务
            mStaues = "";
        }
    }
}
